package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * 意见反馈S
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener {

    private InputMethodManager manager;
    private RelativeLayout re_back;
//    private SelectPhotoAdapter selectPhotoAdapter;
//    private SelectPhotoAdapter.PhotoItem photoItem;
    private EditText ed_content;
    private TextView tv_submit;

    @Override
    public int intiLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initView() {

        manager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        re_back=findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        ed_content = findViewById(R.id.ed_content);
        tv_submit = findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

        if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        switch (v.getId()){

            //返回键
            case R.id.re_back:

                finish();
                break;

            //提交
            case R.id.tv_submit:

                String content = ed_content.getText().toString().trim();

                if (content.length() == 0){

                    toastShort("请填写反馈内容");
                }else {
                    showLoading();
                    getData(content);
                }
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){

                case 1:

                    try{

                        JSONObject jsonObject = new JSONObject((String) msg.obj);

                        if (jsonObject.optString("code").equals("0")){
                           dismissLoading();
                           toastShort("反馈成功");

                            finish();
                        }else {

                            toastShort("反馈失败");
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(FeedBackActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(FeedBackActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(FeedBackActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private void getData(String content){

        HashMap<String,Object> map = new HashMap<>();
        map.put("token", SpUtils.get(FeedBackActivity.this,"token",""));
        map.put("mark", SpUtils.get(FeedBackActivity.this,"mark",1));
        map.put("fkxx",content);

        OkHttpUtils.getNetLoginWork(this, Constant.baseurl+"online/user/feedBack",map,handler);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
